#!/usr/bin/env python
# coding:utf-8
from PoboAPI import *
import datetime
import numpy as np
#统计中国商品期权所有挂牌合约中昨日持仓金额并排序
#get the commodity options value of previous trading date 
#用poboquant python实现，在poboquant上运行，如果有问题 可加群 726895887 咨询
#开始时间，用于初始化一些参数
def OnStart(context) :
    print "I\'m starting..."
    #登录交易账号，需在主页用户管理中设置账号，并把期货测试替换成您的账户名称
    context.myacc = None

    g.optionsvarietylist=["m","c","CF","SR","cu","ru"]
    if context.accounts.has_key("回测期货") :
        print "登录交易账号[回测期货]"
        if context.accounts["回测期货"].Login() :
            context.myacc = context.accounts["回测期货"]

def OnMarketQuotationInitialEx(context, exchange,daynight):
    if exchange != 'SHFE':
        return
    #获取主力合约
    g.code = GetMainContract('SHFE', 'rb',20)
    #订阅K线数据，用于驱动OnBar事件
    SubscribeBar(g.code, BarType.Day) 
    
    g.resultcalls={}
    g.resultputs={}
    
#实时行情事件，当有新行情出现时调用该事件Ex
def OnBar(context,code,bartype) :
    #过滤掉不需要的行情通知
    if code != g.code:
        return
    cur_trade_day = GetCurrentTradingDate('SHFE')
    pre_trade_time = GetPreviousTradingDate('SHFE',cur_trade_day)#get prevous trading date
    print "previous trading day "+str(pre_trade_time)
    for product in g.optionsvarietylist:
        
        contractlist=GetFuturesContracts(GetExchangeByVariety(product),product)
        opcsum=0
        oppsum=0
        for contract in contractlist:
            
            opclist = GetOptionContracts(contract,None,0)#for calls
            opplist = GetOptionContracts(contract,None,1)#for calls
            

            for opc in opclist:

                
                
                option = PBObj()
               
                option.EndDate = pre_trade_time
                option.Count=2
                klinedata = GetHisData(opc, BarType.Day, option)
                
                if klinedata:
                    value=klinedata[-1].close*klinedata[-1].amount*GetContractInfo(contract)["合约乘数"]

                    #print str(opc) +"  value "+str(value)

                    opcsum=opcsum+value
            for opp in opplist:

                option = PBObj()
               
                option.EndDate = pre_trade_time
                option.Count=2
                klinedata = GetHisData(opp, BarType.Day, option)
                
                if klinedata:
                
                    value=klinedata[-1].close*klinedata[-1].amount*GetContractInfo(contract)["合约乘数"]

                    #print str(opp) +"  value "+str(value)

                    oppsum=oppsum+value    
                
        g.resultcalls[product]=opcsum
        g.resultputs[product]=oppsum 
        
                    
    print "calls "+str(sorted(g.resultcalls.items(),key=lambda item:item[1]))
    print "puts "+str(sorted(g.resultputs.items(),key=lambda item:item[1])) 
